<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>添加菜单</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/css/oksub.css">
    <script type="text/javascript" src="/static/lib/loading/okLoading.js"></script>

    <link type="text/css" rel="stylesheet" href="/static/css/metroStyle.css">
    <link type="text/css" rel="stylesheet" href="/static/css/treeSelectStyle.css">
    <script src="/static/js/jquery-1.11.2.min.js"></script>
    <script type="text/javascript" src="/static/js/jquery.ztree.all.js"></script>
    <script type="text/javascript" src="/static/js/treeSelect.2.0.js"></script>
    <script type="text/javascript" src="/static/js/xm-select.js"></script>
    <script type="text/javascript" src="/static/js/xeutils.js"></script>
</head>
<body>
<div class="ok-body">
    <!--form表单-->
    <form class="layui-form layui-form-pane ok-form">
        {% csrf_token %}
        <div class="layui-row layui-col-space10">
            <div class="layui-inline layui-col-md6">
            <label class="layui-form-label">菜单名称<span style="color: red">*</span></label>
            <div class="layui-input-block">
                <input type="text" name="name" id="name" placeholder="请输入菜单名称" autocomplete="off" class="layui-input" maxlength="16"
                       lay-verify="required">
                <input type="text" name="id" id="id" class="layui-input" style="display: none">
            </div>
        </div>
            <div class="layui-inline layui-col-md6">
            <label class="layui-form-label">菜单指向<span style="color: red">*</span></label>
            <div class="layui-input-block">
                <input type="text" name="path" id="path" placeholder="请输入菜单指向" autocomplete="off" class="layui-input"
                       lay-verify="required">
            </div>
        </div>
        </div>
        <div class="layui-row layui-col-space10">
            <div class="layui-inline layui-col-md6">
            <label class="layui-form-label">菜单类型</label>
                <div class="layui-input-block">
                    <input type="radio" name="menu_type" lay-filter="href" value="0"  title="目录">
                    <input type="radio" name="menu_type" lay-filter="href" value="1" title="菜单">
                    <input type="radio" name="menu_type" lay-filter="href" value="2"  title="按钮">
                </div>
            </div>
            <div class="layui-inline layui-col-md6">
            <label class="layui-form-label">图标</label>
            <div class="layui-input-block">
                <input type="text" name="icon" id="icon" id="iconShow" placeholder="请输入菜单图标" autocomplete="off" class="layui-input"
                    >
            </div>
            </div>
        </div>
        <div class="layui-row layui-col-space10">
            <div class="layui-inline layui-col-md6">
            <label class="layui-form-label">菜单排序</label>
            <div class="layui-input-block">
                <input type="text" name="sort" id="sort" placeholder="请输入菜单排序" autocomplete="off" class="layui-input"
                    >
            </div>
        </div>
        <div class="layui-inline layui-col-md6">
            <label class="layui-form-label">上级菜单</label>
             <div class="layui-input-block" name="menu_pname" id="menu_pname">

             </div>
        </div>
        </div>
        <div class="layui-row layui-col-space10" style="padding-top: 10px;">
            <div class="layui-inline layui-col-md6" >
            <label class="layui-form-label">key</label>
            <div class="layui-input-block">
                <input type="text" name="key" id="keyShow" placeholder="请输入按钮key" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item layui-form-text" style="padding-top: 10px;">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea name="remark_describe" placeholder="请输入备注内容" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="edit">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary" id="close">关闭</button>
            </div>
        </div>
    </form>
</div>
<!--js逻辑-->
<script src="/static/lib/layui/layui.js"></script>
<script>
    let data1;
    function child(d) {
        data1 = d;

    layui.use(["element","form", "okUtils", "okLayer"], function () {
        let $ = layui.$;
        let form = layui.form;
        let okUtils = layui.okUtils;
        let okLayer = layui.okLayer;
        let tree = layui.tree;
        okLoading.close();
        $('#id').val(data1.id);
        $('#name').val(data1.title);
        $('#path').val(data1.path);
        $('#menu_type').val(data1.menu_type);
        $('#menu_pname').val(data1.menu_pname);
        $('#icon').val(data1.icon);
        $('#remark_describe').val(data1.remark_describe);
        $('#sort').val(data1.sort);
        if(data1.menu_type==0){
            $('#keyShow').addClass('layui-disabled');
                $('#iconShow').removeClass('layui-disabled');
                $('div.layui-unselect.layui-form-radio')[0].click();
                $("input[type=radio][name='menu_type']").attr('disabled',true);
        }else if(data1.menu_type==1){
            $('#keyShow').removeClass('layui-disabled');
                $('#iconShow').removeClass('layui-disabled');
                $('div.layui-unselect.layui-form-radio')[1].click();
                $("input[type=radio][name='menu_type']").attr('disabled',true);
             
        }else{
            $('#iconShow').addClass('layui-disabled');
                $('#keyShow').removeClass('layui-disabled');
                $('div.layui-unselect.layui-form-radio')[2].click();
                $("input[type=radio][name='menu_type']").attr('disabled',true);
             
        }
        form.render();
        // 回显下拉框
        var initvalue;
        if(data1.parent===null){
            initvalue=0;
        }else{
            initvalue=data1.parent
        }
         var demo3 = xmSelect.render({
                el: '#menu_pname',
                model: {label: {type: 'text'}},
                radio: true,
                clickClose: true,
                layVerify:'verMenuName',
                layVerType:'message',
                initValue:[initvalue],
                prop:{
	                name:'title',
                    value:'id',
                },
                tree: {
                    show: true,
                    strict: false,
                    expandedKeys: [-1],
                },
                height: 'auto',
                data() {
                    return []
                }
            });
            $.ajax({
                url: "{% url 'system:getMenuData' %}",
                type: "GET",
                success: function (ret) {
                    var node = ret.data;
                    demo3.update({
                        data: XEUtils.toArrayTree(node,{ id:'id',parentKey: 'parent', strict: true, children:'children' }),
                        autoRow: true,
                    })
                },
                error: function () {
                }
            });
            form.verify({
                verMenuName:function(value,item){
                    if(data1.id==value){
                        return '不能选择自己为上级菜单!';
                    }
                }
            })

        // 修改动作
        form.on("submit(edit)", function (data) {
            okUtils.ajax("/system/editMenuData/" + data.field.id, "post", data.field, true).done(function (response) {
                okLayer.greenTickMsg(response.msg, function () {
                    parent.layer.close(parent.layer.getFrameIndex(window.name));
                });
            }).fail(function (error) {
                console.log(error)
            });
            return false;
        });
        $("#close").click(function () {
            let index = parent.layer.getFrameIndex(window.name); //获取当前窗口的name
            parent.layer.close(index);

        });
    })};
</script>
</body>
</html>
